<?php
echo '<h1>Gestion des flottes</h1>';

if (isset($_POST['army'], $_POST['target'], $_POST['order']) && is_array($_POST['army']))
{
	echo '<section><h2>Traitement de la gestion d\'une armé</h2>';
	$order = (string)$_POST['order'];
	$target = (int)$_POST['target'];
	echo '<p>ordre : '.$order.'</p>';

	try {
		$move = U1_Move::getMoving($order, $target, new U1_Removable_Resource(array(2, 2, 3 => 1)));

		$army = new U1_Units;
		foreach ($_POST['army'] as $group => $count)
			$army->add(new U1_Unit((int)$group, (int)$count));

		$moveArmy = new U1_Move_Units($planets->getCurrentId(), $army, $move);

		echo '<p class="information">'.
			(($moveArmy->send())
				? 'départ :'."\n".U1_Display_Move::getMessageStructure(U1_Army::INDEX, $army->toArray()).(($move instanceof U1_Move_Transport) ? "\n".U1_Display_Move::getMessageStructure(U1_Resource::INDEX, $move->getRemovable('U1_Removable_Resource')->getQuantities()) : '')
				: 'Impossible d\'envoyé les vaisseaux')
			.'</p>';
	}
	catch (Exception $e)
	{
		echo '<p class="information error">'.$e->getMessage().'</p>';
	}

	echo '</section>';
}


$armyLevelGroups = $planets->getCurrentInstance(U1_Army::INDEX)->getLevelSumGroups();
SF_Process_Array::removeEmptyValue($armyLevelGroups);

echo U1_Display_Move::getMessageStructure(U1_Army::INDEX, $armyLevelGroups)
.U1_Display_Move::getMessageStructure(
	U1_Resource::INDEX,
	$planets->getCurrentInstance(U1_Resource::INDEX)
		->getQuantities()
)
.'<h2>Envoyer des vaisseaux</h2><form method="post" action=""><label for="target">Cible :</label> <input id="target" type="number" name="target" value="'.(isset($_GET['target']) ? (int)$_GET['target'] : 0).'" /><fieldset><legend>Vaisseaux</legend>';
foreach ($armyLevelGroups as $group => $count)
{
	echo '<p><label for="army'.$group.'">'.U1_Display_Army::getName($group).' :</label> <input  id="army'.$group.'"type="number" name="army['.$group.']" />/'.$count.'</p>';
}
echo '</fieldset><p>Ordre <select name="order"><option value="attack">attaquer</option><option value="spy">espionner</option><option value="transport">transporter</option><option value="base">baser</option><option value="colonize">coloniser</option></select></p><p><input type="submit" /></p></form>';


//information sur les requête sql
// $queries = SW_DAO::getQueriesInfo();
// $method = $queries_error = array();
// foreach ($queries as $k => &$e)
// {
// 	if(count($e[1]) > 1)
// 		$queries_error[] = $k.' => '.implode(',' ,$e[1]);
// 	$e = $e[0];
//
// 	$e = substr($e, 0, strpos($e, ' '));
// 	if (!isset($method[$e]))
// 		$method[$e] = 1;
// 	else
// 		++$method[$e];
// }
// var_dump($queries, $queries_error, $method);
//
// echo "\n".'Nombre de requete préparé : '.count($queries)."\nErreur : ".count($queries_error);